<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!--    商品类型列表-->
    <el-table v-loading="loading" :data="productAttrList" @selection-change="handleSelectionChange">
      <el-table-column width="100" label="编号" align="center" prop="productAttrId"/>
      <el-table-column label="商品类型名称" align="center" prop="productAttrName"/>
      <el-table-column label="属性数量" align="center" prop="productStatsCount"/>
      <el-table-column label="参数数量" align="center" prop="productParameterCount"/>
      <el-table-column label="设置" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="plain" @click="getStatsList(scope.row,0)" v-hasPermi="['system:post:edit']">
            属性列表
          </el-button>
          <el-button size="mini" type="plain" @click="getStatsList(scope.row,1)" v-hasPermi="['system:post:remove']">
            参数列表
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:post:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:post:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="[3, 5, 7, 10]"
      @size-change="handleSizeChange"
      :page.sync="listRequest.pageNum"
      :limit.sync="listRequest.pageSize"
      @pagination="getList"/>
    <!-- 添加或修改类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="productAttr" :rules="rules" label-width="80px">
        <el-form-item label="类型名称" prop="productAttrName">
          <el-input v-model="productAttr.productAttrName" placeholder="请输入类型名称"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 参数/属性值列表对话框 -->
    <el-dialog :title="titleStatsList" :visible.sync="openStatsList" width="800px" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAddStats"
            v-hasPermi="['system:post:add']"
          >新增
          </el-button>
        </el-col>
      </el-row>
      <el-table :data="productStatsList">
        <el-table-column property="productStatsId" label="编号" width="100"></el-table-column>
        <el-table-column property="name" label="属性名称" width="80"></el-table-column>
        <el-table-column property="productAttrName" label="商品类型" width="100"></el-table-column>
        <el-table-column property="check" label="属性可选" width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.check == 0">唯一</span>
            <span v-if="scope.row.check == 1">单选</span>
            <span v-if="scope.row.check == 2">复选</span>
          </template>
        </el-table-column>
        <el-table-column property="enter" label="属性值录入方式" width="110">
          <template slot-scope="scope">
            <span v-if="scope.row.enter == 0">手工录入</span>
            <span v-if="scope.row.enter == 1">从列表中选取</span>
          </template>
        </el-table-column>
        <el-table-column property="selectable" label="可选值列表 " width="100"></el-table-column>
        <el-table-column property="isSort" label="排序" width="100"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="updateStats(scope.row)"
                       v-hasPermi="['system:post:edit']">修改
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="deleteStats(scope.row)"
                       v-hasPermi="['system:post:remove']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 添加或修改属性对话框 -->
    <el-dialog :title="titleStats" :visible.sync="openStats" width="800px" append-to-body>
      <el-form ref="formStats" :model="productStats" :rules="formStats" label-width="80px">
        <el-form-item label="属性名称" prop="name">
          <el-input v-model="productStats.name" placeholder="请输入属性名称"/>
        </el-form-item>
        <el-form-item label="商品类型" prop="productAttrId">
          <el-select v-model="productStats.productAttrId" placeholder="请选择商品类型">
            <el-option
              v-for="item in productAttrList"
              :key="item.productAttrId"
              :label="item.productAttrName"
              :value="item.productAttrId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类筛选样式" prop="style">
          <el-radio v-model="productStats.style" :label="0">普通</el-radio>
          <el-radio v-model="productStats.style" :label="1">颜色</el-radio>
        </el-form-item>
        <el-form-item label="能否进行检索" prop="search">
          <el-radio v-model="productStats.search" :label="0">不需要检索</el-radio>
          <el-radio v-model="productStats.search" :label="1">关键词检索</el-radio>
          <el-radio v-model="productStats.search" :label="2">范围检索</el-radio>
        </el-form-item>
        <el-form-item label="商品属性关联" prop="relevance">
          <el-radio v-model="productStats.relevance" :label="0">是</el-radio>
          <el-radio v-model="productStats.relevance" :label="1">否</el-radio>
        </el-form-item>
        <el-form-item label="属性是否可选" prop="check">
          <el-radio v-model="productStats.check" :label="0">唯一</el-radio>
          <el-radio v-model="productStats.check" :label="1">单选</el-radio>
          <el-radio v-model="productStats.check" :label="2">复选</el-radio>
        </el-form-item>
        <el-form-item label="属性值的录入方式" prop="enter">
          <el-radio v-model="productStats.enter" :label="0">手工录入</el-radio>
          <el-radio v-model="productStats.enter" :label="1">从下面列表中选择</el-radio>
        </el-form-item>
        <el-form-item label="属性值可选值列表" prop="selectable">
          <el-input v-model="productStats.selectable" placeholder="请输入可选值"/>
        </el-form-item>
        <el-form-item label="是否支持手动新增" prop="manualAddition">
          <el-radio v-model="productStats.manualAddition" :label="0">是</el-radio>
          <el-radio v-model="productStats.manualAddition" :label="1">否</el-radio>
        </el-form-item>
        <el-form-item label="排序属性" prop="isSort">
          <el-input v-model="productStats.isSort" placeholder="请输入排序属性"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitProductStats">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>

import {
  addProductAttr, addProductStats,
  deleteProductAttr, deleteProductStats, findByAttrIdList,
  findList,
  updateProductAttr, updateProductStats
} from "../../../api/product/productAttr/productAttr";
import {updateProductBrand} from "../../../api/product/productBrand/productBrand";

export default {
  name: "ProductAttr",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/file/upload", // 上传的图片服务器地址
      //上传图片
      fileList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品类型表格数据
      productAttrList: [],
      // 弹出层标题
      title: "",
      // 弹出层(属性值)标题
      titleStats: "",
      // 弹出层(属性值/参数列表)标题
      titleStatsList: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      listRequest: {
        productAttrId: undefined,
        type: 0,
        pageNum: 1,
        pageSize: 5
      },
      statsListRequest:{
        productAttrId: undefined,
        type: 0,
        pageNum: 1,
        pageSize: 5
      },
      // 表单参数
      productAttr: {},
      // 商品对应属性/参数总条数
      statsTotal: 0,
      //商品对应属性/参数列表对话框
      openStatsList: false,
      //商品对应属性/参数添加/修挂对话框
      openStats: false,
      //商品对应属性/参数
      productStatsList: [],
      //属性/参数
      productStats: {},
      // 表单校验
      rules: {
        productAttrName: [
          {required: true, message: "类型名称不能为空", trigger: "blur"}
        ]
      },
      // 表单校验
      formStats: {
        name: [
          {required: true, message: "属性名称不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    resetStats() {
      this.productStats = {
        productStatsId: 0,//商品属性编号
        name: '',//属性姓名
        productAttrId: undefined,//商品类型
        style: undefined,//分类筛选样式: 0:普通 1:颜色
        search: undefined,//能否进行检索 0:不需要检索 1:关键词检索 2:范围检索
        relevance: undefined,//商品属性关联 0:是 1:否
        check: undefined,//属性是否可选 0:唯一 1:单选 2:复选
        enter: undefined,//属性值录入方式 0:手工录入 1:从下面列表中选择
        selectable: '',//属性值可选值列表
        manualAddition: undefined,//是否支持手动新增 0:是 1:否
        isSort: undefined,//排序
      }
    },

    updateBrandManufacturer(row) {
      console.log(row);
      if (row.productBrandId != undefined) {
        updateProductBrand(row).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        });
      }
    }, handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.listRequest.pageSize = val;
      console.log(this.listRequest);
      this.getList()
    },
    /** 查询商品类型列表 */
    getList() {
      this.loading = true;
      findList(this.listRequest).then(response => {
        this.productAttrList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 查询对应属性值/参数列表 */
    getStatsList(row, type) {
      this.openStatsList = true;
      this.statsListRequest.type = type;
      this.titleStatsList = '参数列表'
      if (type === 0) {
        this.titleStatsList = '属性值列表'
      }
      this.statsListRequest.productAttrId = row.productAttrId;
      findByAttrIdList(this.statsListRequest).then(response => {
        this.productStatsList = response.data.list
        this.statsTotal = response.data.total
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.productAttr = {
        productAttrId: 0,
        productAttrName: ''
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.productAttr = row;
      console.log(this.productAttr);
      this.open = true;
      this.title = "修改商品类型";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品类型";
    },

    /** 修改属性按钮操作 */
    updateStats(row) {
      this.resetStats()
      this.productStats = row;
      this.openStats = true;
      this.titleStats = "修改商品属性";
    },
    /** 新增属性按钮操作 */
    handleAddStats() {
      this.resetStats();
      this.openStats = true;
      this.titleStats = "添加商品属性";
    },
    /** 提交按钮 */
    submitForm: function () {
      console.log(this.productAttr)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.productAttr.productAttrId != 0) {
            updateProductAttr(this.productAttr).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductAttr(this.productAttr).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮添加/修改属性值 */
    submitProductStats: function () {
      console.log(this.productStats)
      this.$refs["formStats"].validate(valid => {
        if (valid) {
          if (this.productStats.productStatsId != 0) {
            updateProductStats(this.productStats).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openStats = false;
              findByAttrIdList(this.listRequest).then(response => {
                this.productStatsList = response.data.list
                this.statsTotal = response.data.total
              });
              this.getList();
              this.getList();
            });
          } else {
            this.productStats.type=0;
            if (this.titleStatsList === '参数列表'){
              this.productStats.type=1;
            }
            addProductStats(this.productStats).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openStats = false;
              findByAttrIdList(this.listRequest).then(response => {
                this.productStatsList = response.data.list
                this.statsTotal = response.data.total
              });
              this.getList();
            });
          }
        }
      });
    },
    /** 删除类型按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除类型编号为"' + row.productAttrId + '"的数据项？').then(function () {
        return deleteProductAttr(row.productAttrId)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 删除属性按钮操作 */
    deleteStats(row) {
      this.$modal.confirm('是否确认删除属性编号为"' + row.productStatsId + '"的数据项？').then(function () {
        return deleteProductStats(row.productStatsId)
      }).then(() => {
        this.getStatsList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
